package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.bean.ConsultationBean;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.OkHttpUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConsultationDetailActivity extends BaseActivity {


    @BindView(R.id.tv_doctor)
    TextView mTvDoctor;
    @BindView(R.id.tv_date)
    TextView mTvDate;
    @BindView(R.id.tv_patient)
    TextView mTvPatient;
    @BindView(R.id.et_symptom)
    EditText mSymptom;
    @BindView(R.id.et_result)
    EditText mEtResult;
    private ConsultationBean.DataBean mDataBean;

    @Override
    public int setLayoutId() {
        return R.layout.activity_consultation_detail;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        if (getIntent() != null){
            mDataBean = getIntent().getParcelableExtra("data");
            mTvDoctor.setText("医生： "+mDataBean.getName());
            mTvDate.setText("日期： "+mDataBean.getHzrq().substring(0,11 ));
            mTvPatient.setText(mDataBean.getJmName());

            }

    }

    private void save(){
        String result = mEtResult.getText().toString();
        String symptom = mSymptom.getText().toString();
        if (TextUtils.isEmpty(result)) {
            MyToast.show("请填写会诊结果");
        } else if (TextUtils.isEmpty(symptom)) {
            MyToast.show("请填写会诊原因");

        }else {
            HashMap<Object,Object> map = new HashMap<>();
            map.put("id", mDataBean.getId());//会诊id
            map.put("hzyx", symptom);//会诊原因
            map.put("hzyz", result); //会诊结果
           // map.put("hzys", ""); 会诊社区医生
            OkHttpUtils.getNetWork(this, Constant.consultationDetail,map ,handler );
        }

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what){
                case 1:
                    Gson gson = new Gson();
                    DataBean dataBean = gson.fromJson((String) msg.obj,DataBean.class );
                    if ( dataBean.getCode() == 0){
                       MyToast.show("确诊成功");
                       finish();

                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(ConsultationDetailActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(ConsultationDetailActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(ConsultationDetailActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @OnClick(R.id.btn)
    public void onViewClicked() {

        save();
    }
}
